package com.kdtech.analyse.app;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

public class OeeeeAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://baoliao.oeeee.com/index/.*/id/[0-9]*.html.*",
		"http://.*.oeeee.com/m.php[?].*=/.*/.*/.*/[0-9]*/.*/.*/.*/[0-9]*/.*/[0-9]*/",
		"http://3g.oeeee.com/nis/[0-9]*/[0-9]*/[0-9]*m.html.*",
	};

	public static HashMap<String, String> getLinks(String mainUrl)
	{
		HashMap<String, String> map = new HashMap<String, String>();

		String cid = "";
		if(mainUrl.indexOf("//gd")!=-1){
			cid="191";

		}else if(mainUrl.indexOf("//health")!=-1){
			cid="12";
		}else if(mainUrl.indexOf("//edu")!=-1){
			cid="11";
		}else if(mainUrl.indexOf("//finance")!=-1){
			cid="181";
		}else{
			cid = "226";
		}

		String detailUrl = "";
		String realUrl = "http://www.oeeee.com/api/channel.php?m=Js4channelNews&a=latest&cid=" + cid + "&page=1&row=50&callback=newsMoreTpl";

//		UrlMeta meta = CrawlHTML.responseToURL(realUrl);
		UrlMeta meta = null;
		if(meta!= null && StringUtils.isNotBlank(meta.getHtml()))
		{
			Matcher matcher = Pattern.compile("\"linkurl\":\"(.{0,100})\",\"time\"").matcher(meta.getHtml());
			while(matcher.find())
			{
				detailUrl = matcher.group(1);
				detailUrl = detailUrl.replace("\\", "");
				map.put(detailUrl, "");
			}
		}
		return map;
	}

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		title =doc.select("h1").text();
		if(StringUtils.isBlank(title))
		{
			title =doc.select("div.content h2").text();
		}
		if(StringUtils.isBlank(title))
		{
			title =doc.select("div.main-container h1").text();
		}

		date=DateUtils.matchDate(doc.select("span#pubtime_baidu").text());
		if(date==null)
		{
			date=DateUtils.matchDate(doc.select("span.ctime").text());
		}
		if(date==null)
		{
			date=DateUtils.matchDate(doc.select("time").text());
		}
		if(date==null)
		{
			date=DateUtils.matchDate(doc.select("div.main-info").text());
		}

		content =HtmlCleaner.getContentHtml(url, doc.select("div.article-main"));
		if(StringUtils.isBlank(content))
		{
			content =HtmlCleaner.getContentHtml(url, doc.select("div.content p"));
		}
		if(StringUtils.isBlank(content))
		{
			content =HtmlCleaner.getContentHtml(url, doc.select("div.main-content"));
		}
		if(StringUtils.isBlank(content))
		{
			content =HtmlCleaner.getContentHtml(url, doc.select("article"));
		}

		author=JSoupUtils.matchAuthor(doc, "编辑：");
		Elements authorList = doc.select("a.nickname");
		if(authorList.size()>1)
		{
			author = authorList.get(1).text();
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	

	

	
}

